unit fuQExport4DocxEditor;

interface

uses
  Windows, Messages, SysUtils{$IFDEF VCL6}, Variants{$ENDIF}, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ImgList, StdCtrls, ExtCtrls, ComCtrls, ToolWin, Buttons,
  QExport4, QExport4Common, QExport4Docx;

type
  TfmQExport4DocxEditor = class(TForm)
    paButtons: TPanel;
    buOK: TButton;
    buCancel: TButton;
    paFile: TPanel;
    laFileName: TLabel;
    btnSave: TSpeedButton;
    edFileName: TEdit;
    chkShow: TCheckBox;
    chkPrint: TCheckBox;
    paOptions: TPanel;
    pcOptions: TPageControl;
    tsCommon: TTabSheet;
    bvl1: TBevel;
    bvl2: TBevel;
    laSkipRecCount_01: TLabel;
    laSkipRecCount_02: TLabel;
    laCaptionRow: TLabel;
    laExportRecCount_02: TLabel;
    chkAllowCaptions: TCheckBox;
    chkAutoCalcStrType: TCheckBox;
    chkVisibleFields: TCheckBox;
    chkGoToFirsrRec: TCheckBox;
    edSkipRecCount: TEdit;
    edCaptionRow: TEdit;
    rbExportAll: TRadioButton;
    rbExportOnly: TRadioButton;
    edExportRecCount: TEdit;
    tsHeader: TTabSheet;
    meHeader: TMemo;
    tsFooter: TTabSheet;
    meFooter: TMemo;
    tsFormats: TTabSheet;
    laInteger: TLabel;
    laDate: TLabel;
    laDateTime: TLabel;
    laTrue: TLabel;
    laNull: TLabel;
    laFalse: TLabel;
    laFloat: TLabel;
    laTime: TLabel;
    laCurrency: TLabel;
    edInteger: TEdit;
    edDateTime: TEdit;
    edDate: TEdit;
    edTrue: TEdit;
    edNull: TEdit;
    edFalse: TEdit;
    edFloat: TEdit;
    edTime: TEdit;
    edCurrency: TEdit;
    btnResetDefault: TBitBtn;
    btnRestoreInitial: TBitBtn;
    paStylesEdit: TPanel;
    pcAllOptions: TPageControl;
    tsCommonTextStyle: TTabSheet;
    pcCommonTextStyle: TPageControl;
    tsFont: TTabSheet;
    bvl9: TBevel;
    bvl10: TBevel;
    bvl11: TBevel;
    laTextFont: TLabel;
    laTextSize: TLabel;
    btnFontColor: TSpeedButton;
    pbFontColor: TPaintBox;
    bvl12: TBevel;
    btnFontBold: TSpeedButton;
    btnFontItalic: TSpeedButton;
    bvl13: TBevel;
    btnFontUnderline: TSpeedButton;
    btnTextLeft: TSpeedButton;
    btnTextCenter: TSpeedButton;
    btnTextRight: TSpeedButton;
    btnTextJustify: TSpeedButton;
    btnBackgroundColor: TSpeedButton;
    pbBackColor: TPaintBox;
    cbTextFont: TComboBox;
    cbTextFontSize: TComboBox;
    chkUseBackground: TCheckBox;
    paSelector: TPanel;
    pcStyleEditor: TPageControl;
    tsOptions: TTabSheet;
    lvOptions: TListView;
    tsStyles: TTabSheet;
    tlbProcessing: TToolBar;
    buAdd: TToolButton;
    buRemove: TToolButton;
    buSeparator1: TToolButton;
    buMoveUp: TToolButton;
    buMoveDown: TToolButton;
    rgStripStyle: TRadioGroup;
    lvSStyles: TListView;
    tsBorderStyle: TTabSheet;
    bvl14: TBevel;
    laBorderColor: TLabel;
    btnBorderColor: TSpeedButton;
    pbBorder: TPaintBox;
    chkUseBorder: TCheckBox;
    ilButtons: TImageList;
    ilListView: TImageList;
    ColorDialog: TColorDialog;
    dlgSaveFileName: TSaveDialog;
    pmHeaderFooter: TPopupMenu;
    miInsertTab: TMenuItem;
    miClear: TMenuItem;
    btnFontStrikeOut: TSpeedButton;
    chkUseHighlight: TCheckBox;
    cbHighlight: TComboBox;
    cbBorderStyle: TComboBox;
    laBorderStyle: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure chkUseBackgroundClick(Sender: TObject);
    procedure chkUseBorderClick(Sender: TObject);
    procedure cbTextFontChange(Sender: TObject);
    procedure cbTextFontSizeChange(Sender: TObject);
    procedure btnFontColorClick(Sender: TObject);
    procedure btnFontColorMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnFontColorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnFontBoldClick(Sender: TObject);
    procedure btnFontItalicClick(Sender: TObject);
    procedure btnFontUnderlineClick(Sender: TObject);
    procedure btnFontStrikeOutClick(Sender: TObject);
    procedure btnTextLeftClick(Sender: TObject);
    procedure btnTextCenterClick(Sender: TObject);
    procedure btnTextRightClick(Sender: TObject);
    procedure btnTextJustifyClick(Sender: TObject);
    procedure btnBackgroundColorClick(Sender: TObject);
    procedure btnBackgroundColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBackgroundColorMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
   procedure lvOptionsSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure lvSStylesChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure lvSStylesSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure pbFontColorPaint(Sender: TObject);
    procedure pbBackColorPaint(Sender: TObject);
    procedure chkUseHighlightClick(Sender: TObject);
    procedure rbExportAllClick(Sender: TObject);
    procedure buAddClick(Sender: TObject);
    procedure buRemoveClick(Sender: TObject);
    procedure buMoveUpClick(Sender: TObject);
    procedure buMoveDownClick(Sender: TObject);
    procedure pcStyleEditorChange(Sender: TObject);
    procedure cbHighlightChange(Sender: TObject);
    procedure btnBorderColorClick(Sender: TObject);
    procedure btnBorderColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBorderColorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pbBorderPaint(Sender: TObject);
    procedure cbBorderStyleChange(Sender: TObject);
  private
    FExportDocx: TQExport4Docx;
    FHeaderStyle: TDocxCellStyle;
    FCaptionRowStyle: TDocxCellStyle;
    FDataStyle: TDocxCellStyle;
    FFooterStyle: TDocxCellStyle;
    FStripStylesList: TDocxStripStyleList;
    FUseBorder: Boolean;
    FBorder: TDocxBorder;

    FFileName: WideString;
    FEnableFontAndBorder: Boolean;
    FSelectedOptions: TDocxCellStyle;
    procedure SetFileName(const Value: WideString);
    procedure SetExportDocx(const Value: TQExport4Docx);
    procedure SetSelectedOptions(const Value: TDocxCellStyle);
    procedure SetEnableFontAndBorder(const Value: Boolean);
    procedure SetFocusedToSelected(Item: TListItem);
    procedure SetStripStylesNumber(Item: TListItem);
  private
    procedure SetFontAndBorder(Source: TDocxCellStyle);
  public
    procedure ApplyChanges;
    procedure FillStripStyles;
    procedure FillOptions;
    procedure FillOther;
    procedure Default;
    
    property FileName: WideString read FFileName write SetFileName;
    property ExportDocx: TQExport4Docx read FExportDocx
      write SetExportDocx;
    property SelectedOptions: TDocxCellStyle read FSelectedOptions
      write SetSelectedOptions;
    property EnableFontAndBorder: Boolean read FEnableFontAndBorder
      write SetEnableFontAndBorder;
  end;

var
  fmQExport4DocxEditor: TfmQExport4DocxEditor;

function RunQExportDocxEditor(AExport: TQExport4Docx): Boolean;

implementation

uses
  QExport4MSOfficeCommon;
  
{$R *.dfm}

function RunQExportDocxEditor(AExport: TQExport4Docx): Boolean;
begin
  if not Assigned(AExport) then
    raise Exception.Create('Export is not assigned.');

  with TfmQExport4DocxEditor.Create(nil) do
  try
    ExportDocx := AExport;
    Default;
    FillStripStyles;
    FillOptions;
    FillOther;

    Result := ShowModal = mrOk;
    if Result then
      ApplyChanges;
  finally
    Free;
  end;
end;

{ TfmQExport4DocxEditor }

procedure TfmQExport4DocxEditor.FormCreate(Sender: TObject);
begin
  FHeaderStyle := TDocxCellStyle.Create;
  FCaptionRowStyle := TDocxCellStyle.Create;
  FDataStyle := TDocxCellStyle.Create;
  FFooterStyle := TDocxCellStyle.Create;
  FStripStylesList := TDocxStripStyleList.Create(TDocxStripStyle);
  FBorder := TDocxBorder.Create;
end;

procedure TfmQExport4DocxEditor.FormDestroy(Sender: TObject);

  procedure ClearListViewData(ListView: TListView);
  var
    i: Integer;
  begin
    for i := 0 to ListView.Items.Count - 1 do
      if Assigned(ListView.Items[i]) then
        ListView.Items[i].Data := nil;
  end;

begin
  ClearListViewData(lvOptions);
  FHeaderStyle.Free;
  FCaptionRowStyle.Free;
  FDataStyle.Free;
  FFooterStyle.Free;
  
  ClearListViewData(lvSStyles);
  FStripStylesList.Free;
  FBorder.Free;

  if Assigned(FExportDocx) then
    FExportDocx := nil;
end;

procedure TfmQExport4DocxEditor.FormShow(Sender: TObject);
begin
  if Assigned(lvOptions.Items[0]) then
  begin
    lvOptions.Items[0].Focused := True;
    lvOptions.Items[0].Selected := True;
  end;
end;

procedure TfmQExport4DocxEditor.chkUseBackgroundClick(Sender: TObject);
begin
  btnBackgroundColor.Enabled := chkUseBackground.Checked;
  if Assigned(FSelectedOptions) then
    FSelectedOptions.UseBackground := chkUseBackground.Checked;
end;

procedure TfmQExport4DocxEditor.chkUseBorderClick(Sender: TObject);
begin
  laBorderColor.Enabled := chkUseBorder.Checked;
  btnBorderColor.Enabled := chkUseBorder.Checked;
  laBorderStyle.Enabled := chkUseBorder.Checked;
  cbBorderStyle.Enabled := chkUseBorder.Checked;
  FUseBorder := chkUseBorder.Checked;
end;

procedure TfmQExport4DocxEditor.cbTextFontChange(Sender: TObject);
begin
  FSelectedOptions.Font.Name := cbTextFont.Text;
end;

procedure TfmQExport4DocxEditor.cbTextFontSizeChange(Sender: TObject);
begin
  FSelectedOptions.Font.Size := StrToIntDef(cbTextFontSize.Text, 11);
end;

procedure TfmQExport4DocxEditor.btnFontColorClick(Sender: TObject);
begin
  if ColorDialog.Execute then
  begin
    if ColorDialog.Color = -1 then
      ColorDialog.Color := clBlack;
    FSelectedOptions.Font.Color := ColorDialog.Color;
    pbFontColor.Repaint;
  end;
end;

procedure TfmQExport4DocxEditor.btnFontColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbFontColor);
end;

procedure TfmQExport4DocxEditor.btnFontColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbFontColor);
end;

procedure TfmQExport4DocxEditor.btnFontBoldClick(Sender: TObject);
begin
  if btnFontBold.Down then
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style + [fsBold]
  else
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style - [fsBold];
end;

procedure TfmQExport4DocxEditor.btnFontItalicClick(Sender: TObject);
begin
  if btnFontItalic.Down then
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style + [fsItalic]
  else
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style - [fsItalic];
end;

procedure TfmQExport4DocxEditor.btnFontUnderlineClick(Sender: TObject);
begin
  if btnFontUnderline.Down then
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style + [fsUnderline]
  else
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style - [fsUnderline];
end;

procedure TfmQExport4DocxEditor.btnFontStrikeOutClick(Sender: TObject);
begin
  if btnFontStrikeOut.Down then
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style + [fsStrikeOut]
  else
    FSelectedOptions.Font.Style := FSelectedOptions.Font.Style - [fsStrikeOut];
end;

procedure TfmQExport4DocxEditor.btnTextLeftClick(Sender: TObject);
begin
  FSelectedOptions.Alignment := caLeft;
end;

procedure TfmQExport4DocxEditor.btnTextCenterClick(Sender: TObject);
begin
  FSelectedOptions.Alignment := caCenter;
end;

procedure TfmQExport4DocxEditor.btnTextRightClick(Sender: TObject);
begin
  FSelectedOptions.Alignment := caRight;
end;

procedure TfmQExport4DocxEditor.btnTextJustifyClick(Sender: TObject);
begin
  FSelectedOptions.Alignment := caJustify;
end;

procedure TfmQExport4DocxEditor.btnBackgroundColorClick(Sender: TObject);
begin
  if ColorDialog.Execute then
  begin
    if ColorDialog.Color = -1 then
      ColorDialog.Color := clBlack;
    FSelectedOptions.BackgroundColor := ColorDialog.Color;
    pbBackColor.Repaint;
  end;
end;

procedure TfmQExport4DocxEditor.btnBackgroundColorMouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  IncLeftAndTop(pbBackColor);   
end;

procedure TfmQExport4DocxEditor.btnBackgroundColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbBackColor);
end;

procedure TfmQExport4DocxEditor.lvOptionsSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  if Item.Selected then
    SelectedOptions := TDocxCellStyle(Item.Data);
end;

procedure TfmQExport4DocxEditor.lvSStylesChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    buRemove.Enabled := (lvSStyles.ItemFocused.Index >= 0);
    buMoveUp.Enabled := (lvSStyles.ItemFocused.Index > 0);
    buMoveDown.Enabled := (lvSStyles.ItemFocused.Index < lvSStyles.Items.Count - 1)
                         and (lvSStyles.ItemFocused.Index >= 0);
  end else
  begin
    buRemove.Enabled := (lvSStyles.Items.Count > 0);
    buMoveUp.Enabled := False;
    buMoveDown.Enabled := False;
  end;
end;

procedure TfmQExport4DocxEditor.lvSStylesSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  if Item.Selected then
    SelectedOptions := TDocxCellStyle(Item.Data);
end;

procedure TfmQExport4DocxEditor.pbFontColorPaint(Sender: TObject);
begin
  if Assigned(FSelectedOptions) then
    PaintStandardColors(pbFontColor, FSelectedOptions.Font.Color);
end;

procedure TfmQExport4DocxEditor.pbBackColorPaint(Sender: TObject);
begin
  if Assigned(FSelectedOptions) then
    PaintStandardColors(pbBackColor, FSelectedOptions.BackgroundColor);
end;

procedure TfmQExport4DocxEditor.chkUseHighlightClick(Sender: TObject);
begin                                                         
  cbHighlight.Enabled := chkUseHighlight.Checked;
  if Assigned(FSelectedOptions) then
    FSelectedOptions.UseHighlight := chkUseHighlight.Checked;
end;

procedure TfmQExport4DocxEditor.rbExportAllClick(Sender: TObject);
begin
  edExportRecCount.Enabled := rbExportOnly.Checked;
  laExportRecCount_02.Enabled := rbExportOnly.Checked;
end;

procedure TfmQExport4DocxEditor.buAddClick(Sender: TObject);
var
  Num: Integer;
begin
  FStripStylesList.Add;
  lvSStyles.Items.BeginUpdate;
  try
    Num := lvSStyles.Items.Count;
    with lvSStyles.Items.Add do
    begin
      Caption := 'Strip Style ' + IntToStr(Num + 1);
      ImageIndex := 1;
      Data := Pointer(FStripStylesList[FStripStylesList.Count - 1].Options);
    end;
  finally
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4DocxEditor.buRemoveClick(Sender: TObject);
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    FStripStylesList.Items[lvSStyles.ItemFocused.Index].Free;
    FSelectedOptions := nil;
    lvSStyles.Items.BeginUpdate;
    try
      lvSStyles.Selected.Delete;
      ForAllListViewItems(lvSStyles, SetFocusedToSelected, False, True);
      ForAllListViewItems(lvSStyles, SetStripStylesNumber, False, True);
    finally
      lvSStyles.Items.EndUpdate;
    end;
  end;
end;

procedure TfmQExport4DocxEditor.buMoveUpClick(Sender: TObject);
var
  TempStyle: TDocxCellStyle;
begin
  TempStyle := TDocxCellStyle.Create;
  try
    TempStyle.Assign(TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data));
    TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data).Assign(TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Data));
    TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Data).Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Selected := True;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Focused := True;
end;

procedure TfmQExport4DocxEditor.buMoveDownClick(Sender: TObject);
var
  TempStyle: TDocxCellStyle;
begin
  TempStyle := TDocxCellStyle.Create;
  try
    TempStyle.Assign(TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data));
    TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index].Data).Assign(TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Data));
    TDocxCellStyle(lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Data).Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Selected := True;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Focused := True;
end;

procedure TfmQExport4DocxEditor.pcStyleEditorChange(Sender: TObject);
begin
  case pcStyleEditor.ActivePage.Tag of
    1://tsOptions
      if Assigned(lvOptions.Selected) then
        SelectedOptions := TDocxCellStyle(lvOptions.Selected.Data);
    2://tsStripStyles
      if Assigned(lvSStyles.Selected) then
        SelectedOptions := TDocxCellStyle(lvSStyles.Selected.Data)
      else
        SelectedOptions := nil;
    3://tsBorder
      SelectedOptions := nil;
  end;
end;

procedure TfmQExport4DocxEditor.cbHighlightChange(Sender: TObject);
begin
  FSelectedOptions.HighlightColor := TMSHighlightColor(cbHighlight.ItemIndex);
end;

procedure TfmQExport4DocxEditor.btnBorderColorClick(Sender: TObject);
begin
  if ColorDialog.Execute then
  begin
    if ColorDialog.Color = -1 then
      ColorDialog.Color := clBlack;
    FBorder.Color := ColorDialog.Color;
    pbBorder.Repaint;
  end;
end;

procedure TfmQExport4DocxEditor.btnBorderColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbBorder);
end;

procedure TfmQExport4DocxEditor.btnBorderColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbBorder);
end;

procedure TfmQExport4DocxEditor.pbBorderPaint(Sender: TObject);
begin
  PaintStandardColors(pbBorder, FBorder.Color);
end;

procedure TfmQExport4DocxEditor.cbBorderStyleChange(Sender: TObject);
begin
  FBorder.Style := TDocxBorderStyle(cbBorderStyle.ItemIndex);
end;

//******************************************************************************

procedure TfmQExport4DocxEditor.SetFileName(const Value: WideString);
begin
  if FFileName <> Value then
  begin
    FFileName := Value;
    edFileName.Text := Value;
  end;
end;

procedure TfmQExport4DocxEditor.SetExportDocx(const Value: TQExport4Docx);
begin
  FExportDocx := Value;
  FHeaderStyle.Assign(FExportDocx.DocxOptions.HeaderStyle);
  FCaptionRowStyle.Assign(FExportDocx.DocxOptions.CaptionRowStyle);
  FDataStyle.Assign(FExportDocx.DocxOptions.DataStyle);
  FFooterStyle.Assign(FExportDocx.DocxOptions.FooterStyle);
  FStripStylesList.Assign(FExportDocx.DocxOptions.StripStylesList);
  chkUseBorder.Checked := FExportDocx.DocxOptions.UseBorder;
  chkUseBorder.OnClick(chkUseBorder);
  FBorder.Assign(FExportDocx.DocxOptions.Border);
end;

procedure TfmQExport4DocxEditor.SetSelectedOptions(
  const Value: TDocxCellStyle);
begin
  FSelectedOptions := Value;
  SetFontAndBorder(FSelectedOptions);
  EnableFontAndBorder := Assigned(Value);
end;

procedure TfmQExport4DocxEditor.SetEnableFontAndBorder(
  const Value: Boolean);
begin
  if FEnableFontAndBorder <> Value then
  begin
    FEnableFontAndBorder := Value;
    laTextFont.Enabled := Value;
    cbTextFont.Enabled := Value;
    laTextSize.Enabled := Value;
    cbTextFontSize.Enabled := Value;
    btnFontColor.Enabled := Value;
    btnFontBold.Enabled := Value;
    btnFontItalic.Enabled := Value;
    btnFontUnderline.Enabled := Value;
    btnFontStrikeOut.Enabled := Value;
    btnTextLeft.Enabled := Value;
    btnTextCenter.Enabled := Value;
    btnTextRight.Enabled := Value;
    btnTextJustify.Enabled := Value;

    chkUseBackground.Enabled := Value;
    chkUseBackground.OnClick(chkUseBackground);
    chkUseHighlight.Enabled := Value;
    chkUseHighlight.OnClick(chkUseHighlight);
  end;
end;

procedure TfmQExport4DocxEditor.SetFontAndBorder(Source: TDocxCellStyle);
begin
  if Assigned(Source) then
    with Source do
    begin
      cbTextFont.ItemIndex := cbTextFont.Items.IndexOf(Font.Name);
      cbTextFontSize.Text := IntToStr(Font.Size);
      btnFontBold.Down := fsBold in Font.Style;
      btnFontItalic.Down := fsItalic in Font.Style;
      btnFontUnderline.Down := fsUnderline in Font.Style;
      btnFontStrikeOut.Down := fsStrikeOut in Font.Style;
      pbFontColor.Repaint;
      case Alignment of
        caLeft:
          btnTextLeft.Down := True;
        caRight:
          btnTextRight.Down := True;
        caCenter:
          btnTextCenter.Down := True;
        caJustify:
          btnTextJustify.Down := True;
      end;
      chkUseBackground.Checked := UseBackground;
      chkUseBackground.OnClick(chkUseBackground);
      pbBackColor.Repaint;
      chkUseHighlight.Checked := UseHighlight;
      chkUseHighlight.OnClick(chkUseHighlight);
      if UseHighlight then
        cbHighlight.ItemIndex := Integer(HighlightColor);
      cbBorderStyle.ItemIndex := Integer(FBorder.Style);
      pbBorder.Repaint;
    end;
end;

procedure TfmQExport4DocxEditor.ApplyChanges;
begin
  FExportDocx.DocxOptions.HeaderStyle.Assign(FHeaderStyle);
  FExportDocx.DocxOptions.CaptionRowStyle.Assign(FCaptionRowStyle);
  FExportDocx.DocxOptions.DataStyle.Assign(FDataStyle);
  FExportDocx.DocxOptions.FooterStyle.Assign(FFooterStyle);
  FExportDocx.DocxOptions.StripStylesList.Assign(FStripStylesList);
  FExportDocx.DocxOptions.Border.Assign(FBorder);
  FExportDocx.FileName := FileName;
  FExportDocx.ShowFile := chkShow.Checked;
  FExportDocx.PrintFile := chkPrint.Checked;
  FExportDocx.AllowCaptions := chkAllowCaptions.Checked;
  FExportDocx.AutoCalcStrType := chkAutoCalcStrType.Checked;
  FExportDocx.OnlyVisibleFields := chkVisibleFields.Checked;
  FExportDocx.GoToFirstRecord := chkGoToFirsrRec.Checked;
  FExportDocx.SkipRecCount := StrToIntDef(edSkipRecCount.Text, 0);
  FExportDocx.CaptionRow := StrToIntDef(edCaptionRow.Text, -1);
  if rbExportOnly.Checked then
    FExportDocx.ExportRecCount := StrToIntDef(edExportRecCount.Text, 0);
  FExportDocx.Header.Text := meHeader.Text;
  FExportDocx.Footer.Text := meFooter.Text;
  FExportDocx.DocxOptions.StripStyleType := TMSStripStyleType(rgStripStyle.ItemIndex);
  FExportDocx.DocxOptions.UseBorder := FUseBorder;
  FExportDocx.DocxOptions.Border.Assign(FBorder);
  with FExportDocx.Formats do
  begin
    IntegerFormat := edInteger.Text;
    FloatFormat := edFloat.Text;
    DateFormat := edDate.Text;
    TimeFormat := edTime.Text;
    DateTimeFormat := edDateTime.Text;
    CurrencyFormat := edCurrency.Text;
    BooleanTrue := edTrue.Text;
    BooleanFalse := edFalse.Text;
    NullString := edNull.Text;
  end;

end;

procedure TfmQExport4DocxEditor.FillStripStyles;
var
  i: Integer;
begin
  lvSStyles.Items.BeginUpdate;
  try
    lvSStyles.OnSelectItem := nil;
    lvSStyles.Items.Clear;
    for i := 0 to FStripStylesList.Count - 1 do
      with lvSStyles.Items.Add do
      begin
        Caption := 'Strip Style ' + IntToStr(i + 1);
        ImageIndex := 1;
        Data := Pointer(FStripStylesList[i].Options);
      end;
    if Assigned(lvSStyles.Items[0]) then
    begin
      lvSStyles.Items[0].Focused := True;
      lvSStyles.Items[0].Selected := True;
    end;
  finally
    lvSStyles.OnSelectItem := lvSStylesSelectItem;
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4DocxEditor.FillOptions;
const
  cOptions: array [0..3] of WideString = ('Header', 'Caption Row', 'Data', 'Footer');
var
  i: Integer;
begin
  lvOptions.Items.BeginUpdate;
  try
    lvOptions.OnSelectItem := nil;
    lvOptions.Items.Clear;
    for i := 0 to 3 do
      with lvOptions.Items.Add do
      begin
        Caption := cOptions[i];
        ImageIndex := 2;
        case i of
          0: Data := Pointer(FHeaderStyle);
          1: Data := Pointer(FCaptionRowStyle);
          2: Data := Pointer(FDataStyle);
          3: Data := Pointer(FFooterStyle);
        end;
      end;
  finally
    lvOptions.OnSelectItem := lvOptionsSelectItem;
    lvOptions.Items.EndUpdate;
  end;
end;

procedure TfmQExport4DocxEditor.FillOther;
begin
  FileName := FExportDocx.FileName;
  chkShow.Checked := FExportDocx.ShowFile;
  chkPrint.Checked := FExportDocx.PrintFile;
  chkAllowCaptions.Checked := FExportDocx.AllowCaptions;
  chkAutoCalcStrType.Checked := FExportDocx.AutoCalcStrType;
  chkVisibleFields.Checked := FExportDocx.OnlyVisibleFields;
  chkGoToFirsrRec.Checked := FExportDocx.GoToFirstRecord;
  edSkipRecCount.Text := IntToStr(FExportDocx.SkipRecCount);
  edCaptionRow.Text := IntToStr(FExportDocx.CaptionRow);
  if FExportDocx.ExportRecCount > 0 then
  begin
    rbExportOnly.Checked := True;
    edExportRecCount.Text := IntToStr(FExportDocx.ExportRecCount);
  end;
  meHeader.Text := FExportDocx.Header.Text;
  meFooter.Text := FExportDocx.Footer.Text;

  with FExportDocx.Formats do
  begin
    edInteger.Text := IntegerFormat;
    edFloat.Text := FloatFormat;
    edDate.Text := DateFormat;
    edTime.Text := TimeFormat;
    edDateTime.Text := DateTimeFormat;
    edCurrency.Text := CurrencyFormat;
    edTrue.Text := BooleanTrue;
    edFalse.Text := BooleanFalse;
    edNull.Text := NullString;
  end;

//  chkUseBorder.
  cbTextFont.Items.Assign(Screen.Fonts);
  rgStripStyle.ItemIndex := Integer(FExportDocx.DocxOptions.StripStyleType);
end;

procedure TfmQExport4DocxEditor.Default;
begin
  pcStyleEditor.ActivePage := tsOptions;
  pcCommonTextStyle.ActivePage := tsFont;
  pcOptions.ActivePage := tsCommon;
  cbHighlight.ItemIndex := 0;
  EnableFontAndBorder := False;
end;

procedure TfmQExport4DocxEditor.SetFocusedToSelected(Item: TListItem);
begin
  if Item.Focused then Item.Selected := True;
end;

procedure TfmQExport4DocxEditor.SetStripStylesNumber(Item: TListItem);
begin
  Item.Caption := 'Strip Style ' + IntToStr(Item.Index + 1);
end;

end.
